var Personage = function(rawObject,shouldCollide){


    Drawable.call(this,rawObject,shouldCollide);
    Personage.Instances = new Array();
    this.width = 48;
    this.height = 92;
    this.alphaD = 0;
    this.gammaD = 0;
    this.bettaD = 0;
    this.src = "/images/perso/f0.png";
    this.spriteSheet = new Image();
    this.spriteSheetObject = null;
    this.Direction = {
        UP : 0,
        RIGHT : 1,
        DOWN : 2,
        LEFT : 3
    }
    this.spriteIndexX = 0;
    this.spriteIndexY = 92;
    this.nbAnimation = 9;
    this.isAnimated = true;
    this.isShape = false;
    this.level = 0;
    this.money = rawObject.money;
    this.showing = true;
    this.name = rawObject.name;
}

Personage.prototype = {

    stringify : function(){
        return { coordinateX : this.x, coordinateY : this.y,id : this.id, money : this.money }
    },

    load : function(engine, densityMap){

        var eng = engine;
        var self = this;
        var dm = densityMap;
        this.spriteSheet.onload = function(){

            var s = new eng.Sheet({x:0,y:0,z:0}, {alphaD:self.alphaD,
                betaD:self.bettaD,gammaD:self.gammaD}, {w:self.width,h:self.height});
            self.spriteSheetObject = new eng.SheetObject(
                {x:self.x,y:self.y,z:0},
                {alphaD:0,betaD:0,gammaD:45},
                [s],
                {w:self.width,h:self.height,relu:23,relv:45});
            self.spriteSheetObject.sheets[0].context.drawImage(self.spriteSheet,self.spriteIndexX,self.spriteIndexY,48,92,0,0,48,92);
            self.spriteSheetObject.setShadows(false, false);
            self.spriteSheetObject.setDimming(true, true);
            self.spriteSheetObject.sheets[0].canvasChanged();

            if (self.shouldCollide){
                dm.addSheet(self.spriteSheetObject.sheets[0]);
            }

            eng.calc.calculateChangedSheets();
            eng.drawing.drawScene(true);
        }
        this.spriteSheet.src = this.src;

    },
    move : function (keys, engine, densityMap){

        var dx = 0;
        var dy = 0;
        var d = -1;
        if (keys.u) {
            dy = -5;
            d = this.Direction.UP;
        }
        if (keys.d) {
            dy = 5;
            d = this.Direction.DOWN;
        }
        if (keys.l) {
            dx = -5;
            d = this.Direction.LEFT;
        }
        if (keys.r) {
            dx = 5;
            d = this.Direction.RIGHT;
        }

        if (dx != 0)
            dy = 0;
        // get allowed target point. character's height is 20, and character can climb up to 10 pixels
        var targetInfo = densityMap.getTargetPoint(this.spriteSheetObject.centerp, {x:dx, y:dy, z:0}, 92, 10);
        var allowMove = targetInfo.allowMove;
        var targetp = targetInfo.targetp;

        var moved = targetp.x != this.spriteSheetObject.centerp.x ||
            targetp.y != this.spriteSheetObject.centerp.y || targetp.z != this.spriteSheetObject.centerp.z;
        if (allowMove && moved){
            this.animate(d);
            this.spriteSheetObject.setPosition(targetp);
            this.x = targetp.x;
            this.y = targetp.y;
            return targetp;
        }
        return null;

    },

    animate : function(direction){
        this.spriteSheetObject.sheets[0].context.clearRect(0,0,48,92);
        switch(direction){
            case this.Direction.DOWN:
                this.moveDown();
                break;
            case this.Direction.UP:
                this.moveUp();
                break;
            case this.Direction.LEFT:
                this.moveLeft();
                break;
            case this.Direction.RIGHT:
                this.moveRight();
                break;
            default:
                break;

        }

    },

    moveDown : function(){
        var self = this;
        self.spriteSheetObject.sheets[0].context.drawImage(self.spriteSheet,self.spriteIndexX,92,48,92,0,0,48,92);
        self.spriteSheetObject.sheets[0].canvasChanged();
        self.spriteIndexX += self.width;
        if (self.spriteIndexX >= self.width * self.nbAnimation)
            self.spriteIndexX = 0;
    },

    moveUp : function(){
        var self = this;
        self.spriteSheetObject.sheets[0].context.drawImage(self.spriteSheet,self.spriteIndexX,460,48,92,0,0,48,92);
        self.spriteSheetObject.sheets[0].canvasChanged();
        self.spriteIndexX += self.width;
        if (self.spriteIndexX >= self.width * self.nbAnimation)
            self.spriteIndexX = 0;
    },

    moveLeft : function(){
        var self = this;
        self.spriteSheetObject.sheets[0].context.drawImage(self.spriteSheet,self.spriteIndexX,276,48,92,0,0,48,92);
        self.spriteSheetObject.sheets[0].canvasChanged();
        self.spriteIndexX += self.width;
        if (self.spriteIndexX >= self.width * self.nbAnimation)
            self.spriteIndexX = 0;
    },

    moveRight : function(){
        var self = this;
        self.spriteSheetObject.sheets[0].context.drawImage(self.spriteSheet,self.spriteIndexX,644,48,92,0,0,48,92);
        self.spriteSheetObject.sheets[0].canvasChanged();
        self.spriteIndexX += self.width;
        if (self.spriteIndexX >= self.width * self.nbAnimation)
            self.spriteIndexX = 0;
    },

    isSelected : function(engine,puv){

        var ouv = engine.drawing.getPointuv(this.spriteSheetObject.sheets[0].centerp);

        console.log(puv);
        console.log(ouv);


        if (puv.u > ouv.u - 24  &&
            puv.u < ouv.u - 24 + 48  &&
            puv.v > ouv.v - 48  &&
            puv.v < ouv.v - 48 + 96){
            console.log(ouv);
            return true;
        }

        return false;
    }

    ,
    getSheet : function(){
        return  this.spriteSheetObject = null;
    },

    getImage : function(){
        return this.spriteSheet;
    },


    drawInInfo :function(context){
        var self = this;
        context.drawImage(self.spriteSheet,self.spriteIndexX,self.spriteIndexY,48,92,0,0,48,92);
    },
    getCuttingInfo : function(){
        return {
            from : 0,
            to : 0,
            width: 48,
            height : 92
        }
    },

    getInfos : function(){
        var infos = new Array();

        infos["Level"] = this.level;
        infos["Money"] = this.money;

        return infos;
    },

    delete : function(){
        if (this.showing){
            console.log("destroy");
            this.spriteSheetObject.destroy();
            this.showing = false;
        }
    }





}